package com.tan00xu.controller;

import com.tan00xu.utils.CmdOutputInformationUtils;
import com.tan00xu.utils.QiNiuYunUtils;
import com.tan00xu.vo.ErrorCode;
import com.tan00xu.vo.Result;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;

import java.util.UUID;

@RestController
@RequestMapping("/upload")
public class UploadController {

    @Autowired
    private QiNiuYunUtils qiNiuYunUtils;



    @PostMapping("/image")
    public Result uploadImage(@RequestParam("image") MultipartFile file) {
        CmdOutputInformationUtils.error("UploadController下的uploadImage被请求了=>上传图片");
        //获取到原始文件名
        String originalFilename = file.getOriginalFilename();
        //唯一文件名
        String fileName = UUID.randomUUID().toString() + "." + StringUtils.substringAfterLast(originalFilename, ".");
        //文件上传路径
        boolean isUpload = qiNiuYunUtils.uploadImage(file, fileName);
        if (isUpload) {
            CmdOutputInformationUtils.error("文件上传成功地址是："+QiNiuYunUtils.URL + fileName);
            return Result.success("文件上传成功",QiNiuYunUtils.URL + fileName);
        } else {
            return Result.fail(ErrorCode.Upload_ERROR.getCode(),ErrorCode.Upload_ERROR.getMsg());
        }
    }
}
